package com.party.business.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.party.business.domain.*;
import com.party.business.mapper.TKcMapper;
import com.party.business.service.ITBusinessDeptService;
import com.party.business.service.ITBusinessPostService;
import com.party.business.service.ITFjFileService;
import com.party.business.service.ITKcService;
import com.party.common.core.domain.entity.SysUser;
import com.party.common.utils.DateUtils;
import com.party.common.utils.SecurityUtils;
import com.party.common.utils.StringUtils;
import com.party.system.service.ISysPostService;
import com.party.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 课程信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class TKcServiceImpl extends ServiceImpl<TKcMapper, TKc>  implements ITKcService
{
    @Autowired
    private TKcMapper tKcMapper;

    @Autowired
    private ITBusinessPostService businessPostService;

    @Autowired
    private ITBusinessDeptService businessDeptService;



    @Autowired
    private ITFjFileService tFjFileService;

    /**
     * 查询课程信息
     *
     * @param id 课程信息主键
     * @return 课程信息
     */
    @Override
    public TKc selectTKcById(String id)
    {

        TBusinessPost businessPostOLd=new TBusinessPost();
        businessPostOLd.setBusinessId(id);
        List<TBusinessPost> oldpost = businessPostService.selectTBusinessPostList(businessPostOLd);
        TKc  kcdetail=new TKc();
        kcdetail.setId(id);
        kcdetail.setCurrentUserId(SecurityUtils.getUserId());
        TKc tKc = tKcMapper.selectTKcByIdApp(kcdetail);
        // 从 oldpost  过滤出 postId
        List<String> postIds = oldpost.stream().map(TBusinessPost::getPostId).collect(Collectors.toList());
        // List<String> postIds 转成 List<long>
        List<Long> longPostIds = postIds.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
        TBusinessDept businessDeptOLd=new TBusinessDept();
        businessDeptOLd.setBusinessId(id);
        List<TBusinessDept> olddept = businessDeptService.selectTBusinessDeptList(businessDeptOLd);
        List<String> deptIds = olddept.stream().map(TBusinessDept::getDeptId).collect(Collectors.toList());
        List<Long> longDeptIds = deptIds.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
        tKc.setPostList(longPostIds);
        tKc.setDeptList(longDeptIds);
        // 视频信息
        TFjFile  fjFile=new TFjFile();
        fjFile.setBusinessId(id);
        List<TFjFile> fjFiles = tFjFileService.selectTFjFileList(fjFile);
        tKc.setFileList(fjFiles);
        return  tKc;
    }

    /**
     * 查询课程信息列表
     *
     * @param tKc 课程信息
     * @return 课程信息
     */
    @Override
    public List<TKc> selectTKcList(TKc tKc)
    {
        return tKcMapper.selectTKcList(tKc);
    }


    @Autowired
    private ISysPostService postService;


    @Autowired
    private ISysUserService sysUserService;


    @Override
    public List<TKc> selectTKcListApp(TKc tKc)
    {
        SysUser sysUser = sysUserService.selectUserByIdApp(SecurityUtils.getUserId());
        tKc.setCurrentUserId(SecurityUtils.getUserId());
               // 获取当前部门Id
        Long deptId = sysUser.getDeptId();
        if(deptId==null){
            tKc.setCurrDept("888");
        }else{
            tKc.setCurrDept(deptId + "");
        }
        List<Long> psotIds = postService.selectPostListByUserId(SecurityUtils.getUserId());
        // psotIds转成逗号隔开的字符串
        String postIdsStr = psotIds.stream().map(String::valueOf).collect(Collectors.joining(","));
        if(StringUtils.isEmpty(postIdsStr)){
            tKc.setCurrPost("88888");
        }else{
            tKc.setCurrPost(postIdsStr);
        }

        // 工龄
        long gl = sysUser.getGl();
        tKc.setCurrentGl(gl);
        return tKcMapper.selectTKcListApp(tKc);
    }


    /**
     * 新增课程信息
     *
     * @param tKc 课程信息
     * @return 结果
     */
    @Override
    public int insertTKc(TKc tKc)
    {
                tKc.setCreateTime(DateUtils.getNowDate());
            return tKcMapper.insertTKc(tKc);
    }

    /**
     * 修改课程信息
     *
     * @param tKc 课程信息
     * @return 结果
     */
    @Override
    public int updateTKc(TKc tKc)
    {
                tKc.setUpdateTime(DateUtils.getNowDate());
        return tKcMapper.updateTKc(tKc);
    }

    /**
     * 批量删除课程信息
     *
     * @param ids 需要删除的课程信息主键
     * @return 结果
     */
    @Override
    public int deleteTKcByIds(String[] ids)
    {
        return tKcMapper.deleteTKcByIds(ids);
    }

    /**
     * 删除课程信息信息
     *
     * @param id 课程信息主键
     * @return 结果
     */
    @Override
    public int deleteTKcById(String id)
    {
        return tKcMapper.deleteTKcById(id);
    }


    @Override
    public void updatePostAndDept(TKc tKs) throws Exception {
        // 删除之前所有的岗位
        TBusinessPost businessPostOLd=new TBusinessPost();
        businessPostOLd.setBusinessId(tKs.getId());
        List<TBusinessPost> oldpost = businessPostService.selectTBusinessPostList(businessPostOLd);
        for (int i = 0; i <oldpost.size() ; i++) {
            TBusinessPost businessPost1 = oldpost.get(i);
            businessPostService.deleteTBusinessPostById(businessPost1.getId());
        }
        List<Long> postList = tKs.getPostList();
        if(StringUtils.isEmpty(postList)){
            throw  new Exception("请选择岗位");
        }
        for (int i = 0; i < postList.size(); i++) {
            Long s = postList.get(i);
            TBusinessPost  businessPost=new TBusinessPost();
            businessPost.setBusinessId(tKs.getId());
            businessPost.setPostId(s+"");
            businessPostService.save(businessPost);
        }
        // 处理部门
        TBusinessDept businessDept=new TBusinessDept();
        businessDept.setBusinessId(tKs.getId());
        List<TBusinessDept> tBusinessDepts = businessDeptService.selectTBusinessDeptList(businessDept);
        for (int i = 0; i <tBusinessDepts.size() ; i++) {
            TBusinessDept businessDept1 = tBusinessDepts.get(i);
            businessDeptService.deleteTBusinessDeptById(businessDept1.getId());
        }
        List<Long> deptList = tKs.getDeptList();
        if(StringUtils.isEmpty(deptList)){
            throw  new Exception("请选择部门");
        }
        for (int i = 0; i < deptList.size(); i++) {
            Long s = deptList.get(i);
            TBusinessDept  tBusinessDept=new TBusinessDept();
            tBusinessDept.setBusinessId(tKs.getId());
            tBusinessDept.setDeptId(s+"");
            businessDeptService.save(tBusinessDept);
        }

    }

}
